{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "486733ac",
   "metadata": {},
   "source": [
    "# 1. 选用 百度ai开放平台"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8525b50e",
   "metadata": {},
   "source": [
    " ## API 文档介绍（Face++ API）\n",
    "### 本周主要内容：图像识别（认知服务）\n",
    "> 1. 22春_API_人工智能与机器学习_week05\n",
    "> 2. 电子讲义设计者：许智超  \n",
    "# 百度AI开放平台\n",
    "## 通用物体和场景识别\n",
    "## 接口描述\n",
    "## 该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。\n",
    "\n",
    "> 请求示例\n",
    "HTTP 方法：POST\n",
    "\n",
    "> 请求URL\n",
    "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\n",
    "\n",
    ">> 鉴权认证机制(access_token)\n",
    ">> 获取Access Token\n",
    ">> 获取access_token示例代码:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48bb33e7",
   "metadata": {},
   "source": [
    "### 鉴权认证机制(access_token)  \n",
    "1. 获取Access Token\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c82a72bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.93e8f68b0deb0ff10448044d2b91cf21.315360000.1963919109.282335-25869175', 'expires_in': 2592000, 'session_key': '9mzdXRIYqLsZ+N7pKzV5AtenY+pU9/ercj8zDDgJp7TtnNMw5KEZjYDemMB5Yx1jR+2OJ36dfzt/V/VZ9kUZAAGNUrgTOg==', 'access_token': '24.ff743bf73096fabf6c771d5daa8f4258.2592000.1651151109.282335-25869175', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'b530f18b594db9b87e5b146e66f7feb8'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "# 准备url参数（酬载）\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'Y4C4z6kqidLf4ikMqwHoD7qR',\n",
    "    'client_secret':'C3g0KUFcl2aUKhtR7vwSGfyT8B9xrytm',\n",
    "    \n",
    "    \n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "\n",
    "if response:\n",
    "    print(response.json())\n",
    "    access_token = response.json()['access_token']\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f67c774",
   "metadata": {},
   "source": [
    "* 请求参数  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ef51e9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab0a5fb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>error</th>\n",
       "      <th>error_description</th>\n",
       "      <th>解释</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>invalid_client</td>\n",
       "      <td>unknown client id</td>\n",
       "      <td>API Key不正确</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>invalid_client</td>\n",
       "      <td>Client authentication failed</td>\n",
       "      <td>Secret Key不正确</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            error             error_description             解释\n",
       "0  invalid_client             unknown client id     API Key不正确\n",
       "1  invalid_client  Client authentication failed  Secret Key不正确"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 提取网站的表格 因为表格是第一个，所以是[0]\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu')[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "776c9fd6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ddd2a6af",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'params' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-0a1b504d181c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     25\u001b[0m \u001b[1;31m#request_url = request_url + \"?access_token=\" + access_token\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m \u001b[0mheaders\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'content-type'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m'application/x-www-form-urlencoded'\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpost\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrequest_url\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     28\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     29\u001b[0m     \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'params' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('xihu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# access_token = '[调用鉴权接口获取的token]'\n",
    "\n",
    "# 写url的参数（酬载）\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    \n",
    "}\n",
    "\n",
    "#request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "120a963f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f99074d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
